import axios from 'axios';
import { message } from 'antd';
import base from './config';
// const baseURL = 'http://localhost:8080/api/promotion';
const baseURL= `${base}/api/promotion`

// 发布新活动
export const publishPromotion = async (promotionData) => {
  try {
    const response = await axios.post(`${baseURL}/admin/create`, promotionData);
    return response.data;
  } catch (error) {
    console.error('发布促销活动失败:', error);
    throw error;
  }
};

// 编辑活动
export const editPromotion = async (promotionUpdateBO) => {
  try {
    const response = await axios.put(`${baseURL}/admin/update`, promotionUpdateBO);
    return response.data;
  } catch (error) {
    console.error('编辑促销活动失败:', error);
    throw error;
  }
};

// 删除活动
export const deletePromotion = async (id) => {
  try {
    const response = await axios.delete(`${baseURL}/admin/delete/${id}`);
    return response.data;
  } catch (error) {
    console.error('删除促销活动失败:', error);
    throw error;
  }
};

// 获取促销活动列表
export const getPromotionList = async (current = 1, size = 10, type, isActive) => {
  try {
    const response = await axios.get(`${baseURL}/admin/list`, {
      params: { current, size, type, isActive } // Pass the parameters
    });
    return response.data;
  } catch (error) {
    console.error('获取促销活动列表失败:', error);
    throw error;
  }
};

// 启用/禁用活动
export const togglePromotionStatus = async (id, isActive) => {
  try {
    const response = await axios.put(`${baseURL}/admin/toggle-status/${id}`, null, {
      params: { isActive } // 作为查询参数传递
    });
    return response.data; // 返回后端的响应数据
  } catch (error) {
    console.error('启用/禁用促销活动失败:', error);
    throw error;
  }
};



